package com.asia5b.a5mall.order.widget

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.model.bean.OrderListType
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel

import com.asia5b.a5mall.middleware.utils.CurrencyPriceBuilder3
import com.asia5b.a5mall.middleware.utils.DisplayUtils
import com.asia5b.a5mall.middleware.utils.WImageLoader
import com.asia5b.a5mall.order.R
import com.asia5b.a5mall.order.adapter.OrderListImgAdapter
import kotlinx.android.synthetic.main.view_order_goodslist.view.*

/**
 * Created by zhangyuncai on 2020/3/9.
 * 订单张商品列表,当一个/多个商品展示
 */
class OrderGoodsView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    val imgWidth by lazy { DisplayUtils.getScreenWidth(getContext()) / 6 }

    init {
        View.inflate(context!!, R.layout.view_order_goodslist, this)
    }

    fun setGoodsList(item: OrderDetailModel, type: OrderListType, goodsList2: ArrayList<AbsGoodsModel>?, total_weight: String? = null) {
        if (goodsList2.isNullOrEmpty()) {
            this.visibility = View.GONE
        } else {
            this.visibility = View.VISIBLE

            //显示每个包裹的重量
            if (type == OrderListType.APPLY_PACKAGING) {
                tv_orderlist_weight.visibility = View.VISIBLE
                tv_orderlist_weight2.visibility = View.VISIBLE
                tv_orderlist_weight.text = "-"
                tv_orderlist_weight2.text = "-"
                total_weight?.let {
                    tv_orderlist_weight.text = "${it} kg"
                    tv_orderlist_weight2.text = "${it} kg"
                }
            } else {
                tv_orderlist_weight.visibility = View.GONE
                tv_orderlist_weight2.visibility = View.GONE
            }

            var goodsList = goodsList2!!

            //todo 显示图片
            ll_orderlist_mulit_vessel.visibility = View.GONE
            ll_orderlist_single_vessel.visibility = View.GONE


            val goodsRecyclerView = rv_orderlist_goods
            val firstImageview = iv_orderlist_firstimg


            if (goodsList.size == 1) {//当只有一个商品时
                val goodsModel = goodsList.get(0)
                ll_orderlist_single_vessel.visibility = View.VISIBLE
                /*
                重新设置图片宽高
                 */
                val layoutParams = firstImageview.layoutParams as LinearLayout.LayoutParams
                layoutParams.width = imgWidth.toInt()
                layoutParams.height = imgWidth.toInt()
                firstImageview.layoutParams = layoutParams
                WImageLoader.displayRoundImage(goodsModel.absGoodsImageUrl, firstImageview, 5)

                //商品名
                tv_orderlist_firstgoodsname.text = goodsModel.absGoodsName

                //订单价格
                CurrencyPriceBuilder3().run {
                    textView = tv_orderlist_firstgoodsprice
                    price = item.order_price
                    priceList = item.order_price_list
                    build()
                }
                if (type == OrderListType.PAY_DIFF) {//todo 补差价的运单总价无法显示
                    tv_orderlist_firstgoodsprice.visibility= View.GONE
                }
            } else if (goodsList.size > 1) {//当有多个商品时
                val goodsSize = goodsList.size
                ll_orderlist_mulit_vessel.visibility = View.VISIBLE
                val goodsAdapter = OrderListImgAdapter(goodsList)
                goodsRecyclerView.layoutManager = GridLayoutManager(context, 4)!!
                goodsRecyclerView.adapter = goodsAdapter

                //订单价格
                CurrencyPriceBuilder3().run {
                    textView = tv_orderlist_goodstotalprice
                    price = item.order_price
                    priceList = item.order_price_list
                    build()
                }
                if (type == OrderListType.PAY_DIFF) {//todo 补差价的运单总价无法显示
                    tv_orderlist_goodstotalprice.visibility= View.GONE
                }

                //共几件
                tv_orderlist_goodssize.text = String.format(ResString.get(R.string.共_数_件), "${goodsSize}")
            }
        }

    }
}